<template>
  <div class="nav">
    <div
      class="nav-item"
      :class="{select :name === 'index'}"
      @click="to('index')"
    >
      <div class="icon">
        <i class="iconfont icon-shouye"></i>
      </div>
      <div class="take">首页</div>
    </div>

    <div
      class="nav-item"
      :class="{select :name === 'message'}"
      @click="to('message')"
    >
      <div class="icon">
        <i class="iconfont icon-xiaoxi">  <mt-badge size="small" style="z-index:1;position:absolute" v-if="message > 0 ">{{message}}</mt-badge></i>
      </div>
      <div class="take">消息</div>
    </div>

    <div
      class="nav-item"
      :class="{select :name === 'sos'}"
      @click="to('sos')" v-if="role =='1'"
    >
      <div class="icon">
        <i class="iconfont icon-safe"></i>
      </div>
      <div class="take">求救</div>
    </div>

    <div
      class="nav-item"
      :class="{select :name === 'square'}"
      @click="to('square')"
    >
      <div class="icon">
        <i class="iconfont icon-shequ"></i>
      </div>
      <div class="take">社区</div>
    </div>

    <div
      class="nav-item"
      :class="{select :name === 'mine'}"
      @click="to('mine')"
    >
      <div class="icon">
        <i class="iconfont icon-wode"></i>
      </div>
      <div class="take">我的</div>
    </div>

  </div>
</template>

<script>
// import MyNav from './components/nav.vue'
export default {
  data () {
    return {
      role: this.$store.getters['user/role'],
      user_id: this.$store.getters['user/id'],
      message: localStorage.getItem('message')
    }
  },
  created () {
    // 获取月份
    const mm = new Date().getMonth() + 1
    // 获取天数
    const dd = new Date().getDate()

    // console.log('daohang')

    localStorage.setItem('mm', mm)
    localStorage.setItem('dd', dd)
  },
  computed: {

    name () {
      return this.$route.name
    }
  },
  mounted () { // 定时执行访问后台获取新消息,3秒请求一次
    // this.timer = setInterval(this.getMessages, 30000)
  },
  methods: {
    getMessages () {
      if (this.role === '1') {
        const params = {
          id: this.user_id,
          status: 2
        }
        this.$http.post('/getMessagesByfuwu', params).then(res => {
        // console.log(res.data)
          if (res.data.code === '1') {
          // 将消息存到localStorage,全局使用
            localStorage.setItem('message', res.data.data.length)
          // console.log()
          }
        })
      } else if (this.role === '2') {
        const params = {
          id: this.user_id,
          status: 1
        }
        this.$http.post('/getMessages', params).then(res => {
        // console.log(res.data)
          if (res.data.code === '1') {
          // 将消息存到localStorage,全局使用
            localStorage.setItem('message', res.data.data.length)
          // console.log()
          }
        })
      }
    },
    to (name) {
      this.getMessages()
      if (name === this.name) return false
      this.$router.replace({
        name
      })
    }
  }
}
</script>

<style scoped>
.nav {
  font-size: 0.6rem;
  position: fixed;
  /* margin: 10px; */
  bottom: 0;
  height: 1.5rem;
  display: flex;
  width: 100%;
  background: white;
  border-top: 1px solid rgb(95, 93, 93);
}
.nav .nav-item {
  flex-grow: 1;
  color: rgb(155, 143, 143);
  display: flex;
  /* 垂直水平居中 */
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.nav .nav-item.select {
  color: rgb(74, 176, 235);
}

.nav .nav-item .icon .iconfont {
  font-size: 0.68rem;
}
.nav .nav-item .take {
  font-size: 0.40rem;
}
</style>
